package Data_Structure.MapAndSet;

import Data_Structure.MapAndSet.model.HashNode;

import java.util.Objects;

/**
 * @className: HashBuck2
 * @author: 芃
 * @description: 哈希桶2
 * @date: 2023/8/28 10:58
 */
class Person{
    public String name;
    public String ID;

    public Person(String name, String ID) {
        this.name = name;
        this.ID = ID;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name) && Objects.equals(ID, person.ID);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, ID);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", ID='" + ID + '\'' +
                '}';
    }
}
public class HashBuck2<K,V> {
    static class Node<K,V>{
        public K key;
        public V val;
        public Node<K,V> next;

        public Node(K key, V val) {
            this.key = key;
            this.val = val;
        }
    }

    public Node<K,V>[] array = (Node<K, V>[]) new Node[10];

    private final float DEFAULT_LOAD_FACTOR = 0.75f;//负载因子
    public int usedSize;

    public void put(K key,V val){
        int index = key.hashCode() % this.array.length;
        Node<K,V> cur = array[index];
        while (cur != null){
            if (cur.key.equals(key)){
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        //③、走到这，说明该下标下的链表没有这个key，则把这个key放到头位置
        Node<K,V> node = new Node<>(key,val);
        cur = array[index];
        array[index] = node;
        node.next = cur;
        this.usedSize++;
        //④、插入完成后，计算是否超过负载因子
        if (loadFactor() >= this.DEFAULT_LOAD_FACTOR){
            //扩容，重新哈希
            resize();
        }
    }
    public V get(K key){
        int index = key.hashCode() % this.array.length;
        Node<K,V> head = array[index];
        while (head != null){
            if (head.key.equals(key)){
                return head.val;
            }
            head = head.next;
        }
        return null;
    }

    //计算负载因子
    private double loadFactor(){
        return (double) this.usedSize /array.length;
    }
    //扩容->二倍扩容，重新哈希
    private void resize(){
        Node<K,V>[] newArr = (Node<K, V>[]) new Node[array.length * 2];
        for (int i = 0; i < array.length; i++) {
            Node<K,V> cur = array[i];
            while (cur != null){
                int index = cur.key.hashCode() % newArr.length;//计算新的下标
                Node<K,V> curNext = cur.next;
                cur.next = newArr[index];
                newArr[index] = cur;
                cur = curNext;
            }
        }
        this.array = newArr;
    }
    public static void main(String[] args) {
        Person person = new Person("张三","123456");
        Person person1 = new Person("张三","123456");
        System.out.println(person.hashCode());
        System.out.println(person1.hashCode());
        HashBuck2<Person,String> hashBuck2 = new HashBuck2<>();
        hashBuck2.put(person,"你好");
        hashBuck2.put(person1,"您好");
        System.out.println(hashBuck2.get(person));
    }
}
